//
//  CheckVersionManager.m
//  SXH
//
//  Created by Ryan on 16/4/22.
//  Copyright © 2016年 lyss. All rights reserved.
//
#define PARK_VERSION @"/parksource/versionMatch"

#import "CheckVersionManager.h"

@implementation CheckVersionManager
singleton_m(CheckVersionManager)
- (id)init
{
    if (self = [super init])
    {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            
            NSString *str = [[NSString alloc] init];
            
            self.modelArray = [NSMutableArray array];
            
            self.messagesArray = [DBManager selectAllDownloadPark];
            if (_messagesArray) {
                for (int i =0 ; i<[self.messagesArray count]; i ++) {
                    ParkEntity *entiy = self.messagesArray[i];
                    
                    if ([str isEqualToString:@""]) {
                        str = entiy.ID;
                    }else{
                        str = [NSString stringWithFormat:@"%@,%@",str,entiy.ID];
                    }
                }
                [self initNetWorkWithVersion:str];

            }
        });
    }
    return self;
}

-(void)initNetWorkWithVersion:(NSString *)str
{
    NSDictionary * parm = [NSDictionary dictionaryWithObjectsAndKeys:str,@"parkidList", nil ];
    CommonService *service = [[CommonService alloc] init];
    [service getNetWorkData:parm Path:PARK_VERSION Successed:^(id entity) {
        NSDictionary * dict = (NSDictionary *)entity;
        NSArray * arrayList = [dict objectForKey:@"rows"];
        for (int i = 0; i<arrayList.count; i++) {
            DownLoadModel *model =[[DownLoadModel alloc] initWithDictionary:arrayList[i] error:nil];
            [self.modelArray addObject:model];
            [[NSNotificationCenter defaultCenter]postNotificationName:Notification_findmeVC_refresh object:nil];
        }
    } Failed:^(NSInteger errorCode, NSString *message) {
    }];
}

- (NSInteger)getNewPackageNumber
{
    NSInteger count = 0;
    for (ParkEntity *entity in self.messagesArray) {
        for (int i =0 ; i<[self.modelArray count]; i ++) {
            DownLoadModel * model = self.modelArray[i];
            if ([model.park_id isEqualToString:entity.ID]) {
                if (![model.version isEqualToString:entity.source_version])
                {
                    count++;
                }
            }
        }
    }
    return count;
    
}

- (void)relaodCheckManager
{
    NSString *str = [[NSString alloc] init];
    
    self.modelArray = [NSMutableArray array];
    
    self.messagesArray = [DBManager selectAllDownloadPark];
    if (_messagesArray) {
        for (int i =0 ; i<[self.messagesArray count]; i ++) {
            ParkEntity *entiy = self.messagesArray[i];
            
            if ([str isEqualToString:@""]) {
                str = entiy.ID;
            }else{
                str = [NSString stringWithFormat:@"%@,%@",str,entiy.ID];
            }
        }
        [self initNetWorkWithVersion:str];
        
    }
}

@end
